package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.api.MusicApi;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.BaseResponseAlbum;
import com.example.myapplication.entity.BaseResponseSongUrl;
import com.example.myapplication.entity.Data;
import com.example.myapplication.entity.SearchAlbum;
import com.example.myapplication.entity.SearchResult;
import com.example.myapplication.service.MusicPlayService;
import com.example.myapplication.util.TimeUtil;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.ContentValues.TAG;

public class PlayActivity extends AppCompatActivity {
    private boolean flag = true;
    private Context context;
    private SeekBar seekBar;
    private String songID;
    private String singerName;
    private int albumID;
    private Data data;
    private SearchResult result;
    private String songName;
    public String url;
    private MusicPlayService musicPlayService;
    private TextView tvCurTime;
    private TextView tvTime;
    private TextView tvSongName;
    private TextView tvSingerName;
    private SearchAlbum album;
    private ImageView imgAlbum;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_play);
        context = this;

        musicPlayService = new MusicPlayService();

        ImageButton img = findViewById(R.id.img_play);
        tvSingerName = findViewById(R.id.tv_singer);
        tvSongName = findViewById(R.id.tv_song_name);
        tvTime = findViewById(R.id.tv_time);
        imgAlbum = findViewById(R.id.img_album_art);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag == true){
                    img.setImageResource(R.drawable.play3);
                    musicPlayService.pause();
                }else {
                    img.setImageResource(R.drawable.pause3);
                    musicPlayService.start();
                }
                flag = !flag;
            }
        });

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        songID = bundle.getString("songID");
        albumID = bundle.getInt("albumID");

        getCover(albumID);
        tvSongName.setText(bundle.getString("songName"));
        tvSingerName.setText(bundle.getString("singerName"));
        //tvTime.setText(TimeUtil.format(musicPlayService.getCurPosition()));

        getUrl(songID);


        seekBar = findViewById(R.id.seek_bar);

    }

    private void getUrl(String songID) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);

        Call<BaseResponseSongUrl> call = api.getSongUrl(songID);
        call.enqueue(new Callback<BaseResponseSongUrl>() {
            @Override
            public void onResponse(Call<BaseResponseSongUrl> call, Response<BaseResponseSongUrl> response) {
                BaseResponseSongUrl baseResponse = response.body();
                url = baseResponse.getData().get(0).getUrl();
                data = baseResponse.getData().get(0);
                musicPlayService.playMusic(url);
            }

            @Override
            public void onFailure(Call<BaseResponseSongUrl> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void getCover(int id) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);

        Call<BaseResponseAlbum> call = api.getAlbum(id);
        call.enqueue(new Callback<BaseResponseAlbum>() {
            @Override
            public void onResponse(Call<BaseResponseAlbum> call, Response<BaseResponseAlbum> response) {
                BaseResponseAlbum baseResponse = response.body();
                album = baseResponse.getAlbum();

                Glide.with(context)
                        .load(album.getPicUrl())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                        .into(imgAlbum);
            }

            @Override
            public void onFailure(Call<BaseResponseAlbum> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

//    class UpdateProgressThread extends Thread {
//        @Override
//        public void run() {
//            super.run();
//            while (seekBar.getProgress() < seekBar.getMax()) {
//                final int curPosition = musicController.getCurPosition();
//                seekBar.setProgress(curPosition);
//                SystemClock.sleep(1000);
//                mHandler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        tv_music_cur_time.setText(TimeTool.format(curPosition));
//                    }
//                });
//            }
//        }
//    }

}